﻿
//此类是用来封装 Addressables ，还没实现
using UnityEngine.AddressableAssets;
using UnityEngine;
using System;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.AddressableAssets.ResourceLocators;

public class XRes
{
    public static void Initialize(Action callBack)
    {
        var op = Addressables.InitializeAsync();
        op.Completed += (arg)=> {
            callBack();
        };
    }

    public static GameObject CreateInstanceSync(string key)
    {
        var gameObject = Addressables.InstantiateAsync(key).WaitForCompletion();
        return gameObject;
    }

    //key: address or label
    public static void CreateInstance(string key, Action<GameObject> callBack)
    {
        CreateInstanceInternal(key, callBack);
    }
    private static async void CreateInstanceInternal(string key, Action<GameObject> callBack)
    {
        var gameObject = await Addressables.InstantiateAsync(key).Task;
        callBack(gameObject);
    }

    public static bool ReleaseInstance(GameObject ins)
    {
        return Addressables.ReleaseInstance(ins);
    }

    public static void LoadAsset<T>(string key, Action<T> callBack)
    {
        LoadAssetInternal<T>(key, callBack);
    }

    private static async void LoadAssetInternal<T>(string key, Action<T> callBack)
    {
        var op = await Addressables.LoadAssetAsync<T>(key).Task;
        callBack(op);
    }

    public static void ReleaseAsset(UnityEngine.Object obj)
    {
        Addressables.Release(obj);
    }

    public static void LoadScene(string key)
    {
        Addressables.LoadSceneAsync(key);
    }
}
